package Task28;

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class CMD {
	private static String workDir;
	private final static String pathExist = "Path exist.";
	private final static String pathNotExist = "Path not exist.";

	public static void main(String[] args) {
		workDir = "C:\\TEST3";
		Result result = new Result();
		Command command = new Command();

		while (true) {
			command = prompt();
			result = getResult(command);
			if (!result.getMessage().isEmpty())// complex situation.
				complexSituationParser(result);
			else
				printResult(result);
		}
	}

	private static void complexSituationParser(Result result) {
		if (result.getMessage().get(0).equals(pathExist))
			complexSituationExistFile(result);
		if (result.getMessage().get(0).equals(pathNotExist))
			complexSituationNotExistDir(result);
	}

	private static void printResult(Result result) {
		if (result.getErrors().isEmpty()) {
			if (result.getExceptions().isEmpty()) {
				output(result.getData());
			} else {
				System.out.println("[X] Exceptions!");
				output(result.getExceptions());
			}
		} else {
			System.out.println("[!] ERROR!");
			output(result.getErrors());
		}
	}

	private static void output(ArrayList<String> aResult) {
		for (int i = 0; i < aResult.size(); i++) {
			System.out.println(aResult.get(i));
		}
	}

	private static Command prompt() {
		Scanner inp = new Scanner(System.in);
		Command command = new Command();

		System.out.print(workDir + ">");
		String sCommand = inp.nextLine();
		command = parse(sCommand);
		command.setWorkDir(workDir);
		return command;
	}

	private static Command parse(String sCommand) {
		Command command = new Command();
		String[] array = sCommand.split(" ");
		command.setNameCommand(array[0]);
		String[] args = new String[array.length - 1];
		for (int i = 1; i < array.length; i++) {
			args[i - 1] = array[i];
		}
		command.setArgs(args);
		return command;
	}

	private static void cdAction(Result result) {
		if (result.getErrors().isEmpty() && result.getExceptions().isEmpty()) {
			workDir = result.getData().get(0);
		}
	}

	private static Result getResult(Command command) {
		Controller myController = new Controller();
		Result result = new Result();
		result = myController.getResult(command);
		if (command.getNameCommand().equalsIgnoreCase("cd"))
			cdAction(result);
		return result;
	}

	private static void complexSituationExistFile(Result result) {
		System.out
				.println("[?] File already exist! " + result.getData().get(1));
		System.out.println("Rewrite this file? [y/n]");
		Scanner inp = new Scanner(System.in);
		String answ = inp.nextLine();
		if (answ.equalsIgnoreCase("y")) {
			Command command = new Command();
			command.setNameCommand("delete");
			String[] args = new String[1];
			args[0] = result.getData().get(1);
			command.setArgs(args);
			command.setWorkDir(workDir);

			getResult(command);
			restartCommand(result);
		}
	}

	private static void complexSituationNotExistDir(Result result) {
		System.out.println("[?] Directory not exist! "
				+ result.getData().get(1));
		System.out.println("Create this directory? [y/n]");
		Scanner inp = new Scanner(System.in);
		String answ = inp.nextLine();
		if (answ.equalsIgnoreCase("y")) {
			Command command = new Command();
			command.setNameCommand("mkdir");
			String[] args = new String[1];
			File file = new File(result.getData().get(1));
			args[0] = file.getParent();
			command.setArgs(args);
			command.setWorkDir(workDir);

			getResult(command);
			restartCommand(result);
		}
	}

	private static void restartCommand(Result result) {
		Command command = new Command();
		command.setNameCommand(result.getData().get(2));
		String[] args = new String[2];
		args[0] = result.getData().get(0);
		args[1] = result.getData().get(1);
		command.setArgs(args);
		command.setWorkDir(workDir);

		result = getResult(command);
		printResult(result);
	}

}
